package sg.com.techstudio.tafep.validator;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.binding.message.MessageBuilder;
import org.springframework.binding.message.MessageContext;
import org.springframework.binding.validation.ValidationContext;
import org.springframework.stereotype.Component;

import sg.com.techstudio.tafep.dao.CompanyDao;
import sg.com.techstudio.tafep.dao.ConfigDao;
import sg.com.techstudio.tafep.obj.RegisterObj;

@Component
public class RegisterObjValidator extends BaseValidator {
	private static final Log log = LogFactory.getLog(RegisterObjValidator.class);
	
	@Autowired
	private ConfigDao configDao;
	
	@Autowired
	private CompanyDao companyDao;
	
	@Autowired
	private ParticipantObjValidator participantObjValidator;
	
	public void validatePledgerQuestion(RegisterObj r, ValidationContext context) {
		log.debug("validatePledgerQuestion() entry");
		MessageContext messages = context.getMessageContext();
		
//		if(StringUtils.isBlank(c.getMainPlanId()))
//			messages.addMessage(new MessageBuilder().error().source("mainPlanId")
//					.code("required.field").build());
		if(r.getPledger()==null)
			addMessage("pledger", ERRORCODE_REQUIRED_FIELD, messages);
		else if(r.getPledger()) {
			if(StringUtils.isBlank(r.getCompanyName())
					&& StringUtils.isBlank(r.getCompanyNameOther()))
				addMessage("companyName", "required.field", messages);
			else if(!StringUtils.isBlank(r.getCompanyName())
					&& !StringUtils.isBlank(r.getCompanyNameOther()))
				r.setCompanyName(""); //clear when both are filled
			else if(!StringUtils.isBlank(r.getCompanyName())
					&& !companyDao.validCompany(r.getCompanyName()))
				addMessage("companyName", "invalid.companyName", messages);
			r.setSnefMember(false);			
		} else {
			if(!StringUtils.isBlank(r.getSnefId())
					&& !configDao.isValidSnefId(r.getSnefId())) {
				addMessage("snefId", "invalid.snefId", messages);
				r.setSnefMember(false);
			} else
				r.setSnefMember(!StringUtils.isBlank(r.getSnefId()));
		}
	}
	
	public void validateCompanySelect(RegisterObj r, ValidationContext context) {
		log.debug("validateMainContact() entry");
		MessageContext messages = context.getMessageContext();
		
		if(r.getCompanyId()==null)
			messages.addMessage(new MessageBuilder().error().source("companyId")
					.code("required.field").build());
	}
	
	public void validateMainContact(RegisterObj r, ValidationContext context) {
		log.debug("validateMainContact() entry");
		MessageContext messages = context.getMessageContext();
		
		if(StringUtils.isBlank(r.getSalutation()) && StringUtils.isBlank(r.getSalutationOther()))
			addMessage("salutation", "required.selectfield", messages);
		validateEmptyString(r.getFirstName(), "firstName", messages);
		validateEmptyString(r.getLastName(), "lastName", messages);
//		if(StringUtils.isBlank(r.getName()))
//			messages.addMessage(new MessageBuilder().error().source("name")
//				.code("required.field").build());
		if(StringUtils.isBlank(r.getDesignation()))
			messages.addMessage(new MessageBuilder().error().source("designation")
				.code("required.field").build());
		else if(StringUtils.startsWithIgnoreCase(r.getDesignation(), "pirate"))
			messages.addMessage(new MessageBuilder().error().source("designation")
					.code("designation.pirate").build());
		if(StringUtils.isBlank(r.getTelephone()))
			messages.addMessage(new MessageBuilder().error().source("telephone")
				.code("required.field").build());
		else if(!PHONE_PATTERN.matcher(r.getTelephone()).matches())
			messages.addMessage(new MessageBuilder().error().source("telephone")
					.code("invalid.telephone").build());
		else if(validateEmptyString(r.getAreaCode(), "areaCode", messages))
			if(!NumberUtils.isDigits(r.getAreaCode()))
				addMessage("areaCode", "invalid.areaCode", messages);
		validateEmail(r, messages);
		if(r.getPledger()!=null && !r.getPledger())
			validateEmptyString(r.getCompanyName(), "companyName", messages);
		if(StringUtils.isBlank(r.getCompanyAddress()))
			messages.addMessage(new MessageBuilder().error().source("companyAddress")
				.code("required.field").build());
//		if(!r.getPledger()) {
//			if(r.isSnefMember())
//				if(StringUtils.isBlank(r.getSnefId()))
//					messages.addMessage(new MessageBuilder().error().source("snefId")
//							.code("required.field").build());
//				else if(!configDao.isValidSnefId(r.getSnefId()))
//					messages.addMessage(new MessageBuilder().error().source("snefId")
//							.code("invalid.snefId").build());
//		}
//		if(validateEmptyString(r.getPostCode(), "postCode", messages))
//			if(!POSTCODE_PATTERN.matcher(r.getPostCode()).matches())
//				addMessage("postCode", "invalid.postCode", messages);
		if(!r.isOtherCompany() && (r.getNoParticipant()<1 || r.getNoParticipant()>10))
			addMessage("noParticipant", "invalid.noParticipant", messages);
	}
	
	public void validateSummary(RegisterObj r, ValidationContext context) {
		log.debug("validateSummary() entry");
//		MessageContext messages = context.getMessageContext();
		
	}
	
	public void validateParticipantEntry(RegisterObj r, ValidationContext context) {
		log.debug("validateParticipantEntry() entry");
//		MessageContext messages = context.getMessageContext();
		participantObjValidator.validateParticipantList(r.getParticipantList(), context);
	}
	
	protected void validateEmail(RegisterObj r, MessageContext messages) {
		log.debug("validateEmail() entry");
		validateEmail(r.getEmail(), "email", messages);
		log.debug("validateEmail() exit");
	}
}
